function ge(A){return document.getElementById(A)}
function ce(C,B){var A=document.createElement(C);if(B){cOr(A,B)}return A}
function ae(A,B){return A.appendChild(B)}
function ct(A){return document.createTextNode(A)}
function cOr(C,A){for(var B in A){if(typeof A[B]=="object"){if(!C[B]){C[B]={}}cOr(C[B],A[B])}else{C[B]=A[B]}}}

var randnum = Math.round(new Date().getTime()/1000.0);

function include_js(file, doFunction) {
    var html_doc = document.getElementsByTagName('head')[0];
    var js = document.createElement('script');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', file);
    html_doc.appendChild(js);
    js.onreadystatechange = function () {
        if ((js.readyState == 'complete') || (js.readyState == 'loaded')) {
            doFunction();
        }
    }

    js.onload = function () {
        doFunction();
    }
    return false;
}

function cssMenu(divname, jsonfile) {
  include_js(jsonfile+'?'+randnum, function() {
    include_js('/javascripts/menu/css.js?'+randnum, function() {
      doStyles(divname);
      makeMenu(ge(divname), menuJSON['menus'], true);
      sfHover();
    });
  });
}

function sfHover() {
  if (!window.XMLHttpRequest) {
    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++) {
      sfEls[i].onmouseover=function() {
        this.className+="sfhover";
      }
      sfEls[i].onmouseout=function() {
        this.className=this.className.replace(new RegExp("sfhover\\b"), "");
      }
    }
  }
}

function makeMenu(divobj,curmenuJSON,root){
  var list = ae(divobj,ce("ul"))
  for(var C=0,A=curmenuJSON.length;C<A;++C){
    if(curmenuJSON[C]['name']==null){
      continue
    }
    
    var linkobj=ce("a"),listitemobj=ce("li"),tableobj=ce("table"),tdobj=ce("td"),trobj=ce("tr"),tbodyobj=ce("tbody");

    if(curmenuJSON[C]['url']){
      linkobj.href=curmenuJSON[C]['url']
    }else{
      linkobj.href="javascript:;";
      linkobj.style.cursor="default";
      linkobj.style.textDecoration="none";
    }
    ae(linkobj,ct(curmenuJSON[C]['name'])) 
    if(curmenuJSON[C]['subnav']!=null){
      ae(listitemobj,linkobj);
      makeMenu(listitemobj,curmenuJSON[C]['subnav'], false);
      if (root) {
        linkobj.style.background='url('+menuJSON.config.downArrow+') center right no-repeat';
      } else {
        linkobj.style.background='url('+menuJSON.config.rightArrow+') center right no-repeat';
      }
    } else {
      ae(listitemobj, linkobj);
    }
    ae(list, listitemobj);
  }
}
